<?php
declare (strict_types=1);

namespace app\api\controller\personal_conter;

use app\api\model\CommissionFlow;
use app\api\model\GroupMaster;
use app\api\model\UserCoupon;
use app\api\model\Users;
use app\serve\controller\Token;
use think\Request;

class PersonalInformation
{
    /**
     * @param Request $request
     * @return string|\think\response\Json
     * 获取个人信息
     */
    public function getUserInfo(Request $request)
    {
        //获取token
        $token = $request->header('token');
        //校验token合法性
        $res = (new Token())->chekToken($token);
        if ($res['code'] != 1) {
            return json(['error_code' => 10002, 'msg' => $res['msg'], 'data' => ''], 400);
        }
        //从token中获取用户id
        $user_id = $res['data']->user_id;
        //实例化模型
        $userModel = new Users();
        //获取用户的类型
        $userInfo = $userModel->userInfo($user_id);
        if (!$userInfo) {
            return json(['code' => 10004, 'msg' => '暂未查到用户信息,请刷新页面后重试', 'data' => []]);
        }
        $userType = $userInfo->type;
        //判断用户的类型
        //消费者个人信息
        if ($userType === '0') {
            $userInfo = $userModel->userInfo($user_id);
            if (!$userInfo) {
                return json(['code' => 10004, 'msg' => '暂未查到用户信息,请刷新页面后重试', 'data' => []]);
            }
            $userCouponModel = new UserCoupon();
            //用户可用的优惠券数量
            $couponNum = $userCouponModel->userCouponNum($user_id);
            //组装数据
            $data = [
                'id' => $userInfo->id,
                'nickname' => $userInfo->nickname,
                'avator' => $userInfo->avator,
                'mobile' => $userInfo->mobile ? $userInfo->mobile : '绑定手机号',
                'balance' => $userInfo->balance,
                'points' => $userInfo->points,
                'couponNum' => $couponNum
            ];
            return json(['code' => 10001, 'msg' => '请求成功', 'data' => $data]);
        }
        //团长个人信息
        if ($userType === '1') {
            //查询用户表是否有该用户
            $userInfo = $userModel->userInfo($user_id);
            if (!$userInfo) {
                return json(['code' => 10004, 'msg' => '暂未查到用户信息,请刷新页面后重试', 'data' => []]);
            }
            //查询团长表 该用户是否为团长 状态是否正常
            $groupMasterModel = new GroupMaster();
            $groupMasterInfo = $groupMasterModel->getGroupMasterInfo($user_id);
            if (!$groupMasterInfo) {
                return json(['code' => 10004, 'msg' => '暂未查到团长信息,请刷新页面后重试', 'data' => []]);
            }
            //获取团长id
            $master_id = $groupMasterInfo->id;
            //查询出团长的佣金
            $commissionModel = new CommissionFlow();
            //月累计佣金 (已结算+未结算)
            $total_month_commission = $commissionModel->yetMonthCommission($master_id);
            //月待结算佣金
            $wait_month_commission = $commissionModel->waitMonthCommission($master_id);
            //累计佣金 (已结算+未结算)(总)
            $total_commission = $commissionModel->yetTotalCommission($master_id);
            //待结算佣金(总)
            $wait_commission = $commissionModel->waitTotalCommission($master_id);
            //数据组装
            $data = [
                'id' => $master_id,
                'nickname' => $groupMasterInfo->nickname,
                'avator' => $groupMasterInfo->avator,
                'total_month_commission' => $total_month_commission,
                'wait_month_commission' => $wait_month_commission,
                'total_commission' => $total_commission,
                'wait_commission' => $wait_commission
            ];
            return json(['code' => 10001, 'msg' => '请求成功', 'data' => $data]);
        }
        //门店个人信息
        if ($userType === '3') {
            return json(['code' => 10005, 'msg' => '此功能正在开发中...', 'data' => []]);
        }
    }
}
